<template>
  <div id="app">
    <el-card shadow="always">
      {{studentSearch}}
      <!--条件查询-->
      <el-form :inline="true" size="medium" :model="studentSearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="studentSearch.sname" placeholder="学生姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="studentSearch.addr" placeholder="学生住址"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="studentSearch.cid" placeholder="所在班级">
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="search" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="editStudent" icon="el-icon-plus">添加学生</el-button>
        </el-form-item>
      </el-form>
      <!--<el-divider><i class="el-icon-loading"></i></el-divider>-->
      <el-table
          :data="studentList"
          style="width: 100%">
        <el-table-column
            label="学号"
            prop="sid"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="姓名"
            align="center"
            prop="sname">
        </el-table-column>
        <el-table-column
            label="性别"
            align="center"
            prop="sex">
        </el-table-column>
        <el-table-column
            label="年龄"
            align="center"
            prop="age">
        </el-table-column>
        <el-table-column
            label="住址"
            align="center"
            prop="addr">
        </el-table-column>
        <el-table-column
            label="所在班级"
            align="center"
            prop="cname">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      {{student}}
      <el-form :model="student">
        <el-form-item label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="student.sname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生性别" :label-width="formLabelWidth">
          <el-radio v-model="student.sex" label="男">男</el-radio>
          <el-radio v-model="student.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="学生年龄" :label-width="formLabelWidth">
          <el-input v-model="student.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生住址" :label-width="formLabelWidth">
          <el-input v-model="student.addr" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所在班级" :label-width="formLabelWidth" style="width: 1200px">
          <el-select v-model="student.cid" placeholder="请选择所在班级" >
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveStudent">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import studentApi from "@/api/student"
import classesApi from "@/api/classes"
export default {
  data() {
    return {
      studentList: [],      // 当前页的所有学生信息集合
      page:1,               // 当前页
      pageSize:5,           // 每页大小
      total:100,            // 总记录数
      searchStudent:{},     // 要查找的学生信息对象
      studentSearch: {},    // 代表查询条件对象
      classesList: [],
      dialogFormVisible:false,  // 控制对话框的显示隐藏
      formLabelWidth:'100px',   // 对话框提示文本的宽度
      student: {},              // 学生对象（添加或修改）
      title: '添加学生',          // 对话框标题
    }
  },
  created() {
    // 1. 进行分页查询
    // this.findByPage(this.page,this.pageSize)
    // 2. 查询所有班级
    this.findClasses()

    this.search()
  },

  methods: {
    // 1. 查询所有学生
    findStudents(){
      this.$axios.get('/student/list.do')
          .then(res => {
            // this.studentList = res.data;
            console.log("数据：",res.data)
            this.studentList = res.data
          })

      // const {data:res} = await studentApi.findStudents()
      // this.studentList = res
    },
    // 2. 进行分页查询
   async findByPage(){
      // this.$axios.get(`/student/findByPage.do?page=${this.page}&pageSize=${this.pageSize}`)
      //     .then(({data}) => {
      //   console.log("分页数据：",data)
      //   this.studentList = data.rows
      //   this.total = data.total
      // })
     // 若依框架写法：
     const {data:res} = await studentApi.findByPage(this.page,this.pageSize)
     this.studentList = res.rows
     this.total = res.total
    },
    // 3. 查询所有班级
    async findClasses(){
     const {data:res} = await classesApi.findClasses()
     this.classesList = res
    },
    // 4. 条件查询带分页
    async search(){
      const {data} = await studentApi.search(this.page,this.pageSize,this.studentSearch)
      this.studentList = data.rows
      this.total = data.total
    },
    // 5. 编辑学生
    editStudent(){
      this.dialogFormVisible = true
    },
    // 6. 保存学生
    async saveStudent(){
      const {data:res} = await studentApi.save(this.student)
      if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    }
  }
}
</script>
<style>
  .el-card{
    width: 90%;
    //margin: 0 auto;
  }
  #app{
    display: flex;
    justify-content: center;
  }
  .el-pagination{
    text-align: right;
    margin-top: 15px;
  }
</style>
